﻿using Microsoft.Xna.Framework;
using System.Text;
using TShockAPI;

namespace BeanPoints
{
   public class BeanPlayer
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int Points { get; set; }
        public TSPlayer Player { get; set; }

        public BeanPlayer(int id,string name,int points,TSPlayer plr) {
            ID = id;
            Name = name;
            Points = points;
            Player = plr;
        }
        public BeanPlayer(int id, string name, int points)
        {
            ID = id;
            Name = name;
            Points = points;
        }
        /// <summary>
        /// 给该对象添加指定数额的货币
        /// </summary>
        /// <param name="count"></param>
        public void AddPoints(int count) {
            this.Points += count;
            DBManager.UpdatePlayer(this);
        }
        /// <summary>
        /// 减少该对象指定数额的货币
        /// </summary>
        /// <param name="count"></param>
        public void DecreasePoints(int count)
        {
            this.Points -= count;
            DBManager.UpdatePlayer(this);
        }
        /// <summary>
        /// 重置该对象的货币为0
        /// </summary>
        public void ResetPoints() {
            this.Points = 0;
            DBManager.UpdatePlayer(this);
        }
        /// <summary>
        /// 设置该对象的Points为指定的数额
        /// </summary>
        /// <param name="count">数量</param>
        /// <returns></returns>
        public bool ResetPoints(int count) {
            if (count < 0)
            {
                return false;
            }
            else
            {
                this.Points = count;
                DBManager.UpdatePlayer(this);
                return true;
            }  
        }
        public static BeanPlayer GetBeanPlayer(string name) {
           return DBManager.GetPlayer(name);
        }
        public static BeanPlayer GetBeanPlayerNoTP(string name)
        {
            return DBManager.GetPlayerNoTP(name);
        }
        public void ShowChangeInfo(ChangeType type,int count) {
            StringBuilder sb = new StringBuilder();
            string EndLine9 = new string('\n', 9);
            string EndLine15 = new string('\n', 15);
            sb.Append(EndLine9);
            sb.Append("----------\n");
            sb.Append($"类型 : {type}\n");
            sb.Append($"数额 : {count}\n");
            sb.Append("----------\n");
            sb.Append(EndLine15);
            sb.Append(EndLine15);
            sb.Append(EndLine15);
            if (Player!=null)
            {
                Player.SendData(PacketTypes.Status, sb.ToString(), 0, (byte)0);
            }
        }  
        public void SendInfoMessage(string msg) {
            Player.SendMessage(msg,Color.DarkTurquoise);
        }
        public void SendMessage(string msg,Color color) {
            Player.SendMessage(msg,color);
        }
        public void SendSuccessMessage(string msg) {
            Player.SendMessage(msg,Color.MediumAquamarine);
        }
        public void SendErrorMessage(string msg) {
            Player.SendMessage(msg,Color.Crimson);
        }
        public void PriceIntoCoins(int aimcount) {
            if (aimcount >Points||aimcount<0)
            {
                SendMessage("请输入正确数字(0<x<你的余额)",Color.Tomato);
                return;
            }
            int pv, gv, sv, cv;//货币价格的变量声明
            int p=0, g=0, s=0, c=0;//货币数量的变量声明
            pv= ConfigUtils.config.CoinValue["Platinum"];
            gv = ConfigUtils.config.CoinValue["Gold"];
            sv = ConfigUtils.config.CoinValue["Silver"];
            cv= ConfigUtils.config.CoinValue["Copper"];
            if (pv!=0)
            {
                p = aimcount / pv;
                aimcount %= pv;
            }
            if (gv != 0)
            {
                g = aimcount / gv;
                aimcount %= gv;
            }
            if (sv != 0)
            {
                s = aimcount / sv;
                aimcount %= sv;
            }
            if (cv != 0)
            {
                c = aimcount / cv;
                aimcount %= cv;
            }
            if (p == 0 && g == 0 && s == 0 && c == 0)
            {
                SendInfoMessage("你输入的数值不满足转换数值");
            }
            else
            {
                Player.GiveItem(71, c, 0);
                Player.GiveItem(72, s, 0);
                Player.GiveItem(73, g, 0);
                Player.GiveItem(74, p, 0);
                DecreasePoints(aimcount);
                AddPoints(aimcount);
                ShowChangeInfo(ChangeType.收入,aimcount);
                SendSuccessMessage($"成功兑换 [i:71]*{c} [i:72]*{s} [i:73]*{g} [i:74]*{p}");
            }
        }
    }
    public enum ChangeType { 
    收入,
    支出
    }
}
